package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.CustomerBalance;
import com.dianxin.farm.entity.CustomerInfo;
import com.dianxin.farm.entity.EnterpriseInfo;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.mapper.customer.CustomerBalanceMapper;
import com.dianxin.farm.mapper.customer.EnterpriseInfoMapper;
import com.dianxin.farm.response.customer.CustomerInfoResponse;
import com.dianxin.farm.service.CurrentLoginService;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 橱窗信息表 前端控制器
 * </p>
 *
 * @author xukai
 * @since 2020-09-14
 */
@RestController
@RequestMapping(value = "/customerInfo", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@AuthorCheck
@Api(description = "用户信息控制器")
public class CustomerInfoController {

    @Resource
    private CustomerBalanceMapper customerBalanceMapper;
    @Resource
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Resource
    private CurrentLoginService currentLoginService;

    @ApiOperation("获取当前登录账号信息，如果是店主role为100，还将返回用户企业金额信息")
    @RequestMapping(value = "/myInfo")
    @ResponseBody
    @AuthorCheck
    public ApiResponse<CustomerInfoResponse> myInfo() {
        CustomerInfoResponse response=new CustomerInfoResponse();
        CustomerInfo customerInfo=currentLoginService.getCurrentInfo();
        Integer customerId=currentLoginService.getId();
        response.setCustomerInfo(customerInfo);

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("customer_id",customerId);
        CustomerBalance customerBalance=customerBalanceMapper.selectOne(wrapper);
        response.setCustomerBalance(customerBalance.getBalance());

        if (customerInfo.getRole()==100){
            EnterpriseInfo enterpriseInfo=enterpriseInfoMapper.selectById(26);
            response.setEnterpriseBalance(enterpriseInfo.getBalance());
        }

        return ApiResponseUtils.success(response);
    }


}
